/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.screen.testscreen;

import java.util.ArrayList;
import java.util.List;

import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.action.DkBaseAsyncCallback;
import com.daikit.daikit4gxt.client.action.DkBasePagingLoader;
import com.daikit.daikit4gxt.client.log.DkLogger;
import com.daikit.daikit4gxt.client.screen.DkBaseScreenSubPanel;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.propertyaccess.ProductPropertyAccess;
import com.daikit.daikit4gxtsample.shared.bean.ProductData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.sencha.gxt.cell.core.client.form.TextInputCell;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.RowClickEvent;
import com.sencha.gxt.widget.core.client.event.RowClickEvent.RowClickHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.tips.QuickTip;
import com.sencha.gxt.widget.core.client.toolbar.FillToolItem;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;


@SuppressWarnings("javadoc")
public class TestScreenSearchUI extends DkBaseScreenSubPanel
{

	private final DkBasePagingLoader<ProductData, PagingLoadConfig, PagingLoadResult<ProductData>> basePagingLoader;
	private final TextButton createButton;
	private final ListStore<ProductData> pagingStore;
	private final TextButton buttonSearch;
	private final TextField textFieldSearch;

	private final TestScreenController controller;

	public TestScreenSearchUI(final TestScreenController controller)
	{
		setBorders(false);
		setBodyBorder(false);
		setHeadingHtml(Main.gwtSampleI18n().test_screen_title());

		this.controller = controller;

		pagingStore = new ListStore<ProductData>(ProductPropertyAccess.INSTANCE.key());
		pagingStore.setAutoCommit(false);

		// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		// TOP TOOLBAR
		// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

		final ToolBar toolbar = new ToolBar();

		textFieldSearch = new TextField(new TextInputCell()
		{
			@Override
			protected void onEnterKeyDown(final com.google.gwt.cell.client.Cell.Context context, final Element parent,
					final String value, final NativeEvent event, final ValueUpdater<String> valueUpdater)
			{
				super.onEnterKeyDown(context, parent, value, event, valueUpdater);
				controller.doSearch();
			}
		});
		toolbar.add(textFieldSearch);

		buttonSearch = new TextButton(Main.i18n().label_search(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				controller.doSearch();
			}
		});
		buttonSearch.setIcon(Main.icons().file_explore_16());
		toolbar.add(buttonSearch);


		createButton = new TextButton(Main.gwtSampleI18n().create_product(), new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				DkLogger.debug(TestScreen.class, "Click on create Products");
				//				for (final Record record : pagingStore.getModifiedRecords())
				//				{
				//					BaseLogger.debug(TestScreen.class, "Modified record : " + record);
				//				}

			}
		});
		createButton.setIcon(Main.icons().file_plus_16());

		final TextButton sampleEditorAction = new TextButton("Editor Action", new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				DkLogger.debug(TestScreen.class, "Click on create sample editor action");
				controller.doSampleAction();
			}
		});
		sampleEditorAction.setIcon(Main.icons().config_16());

		toolbar.add(new FillToolItem());
		toolbar.add(sampleEditorAction);
		toolbar.add(createButton);

		// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
		// GRID
		// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

		final List<ColumnConfig<ProductData, ?>> columnConfigs = new ArrayList<ColumnConfig<ProductData, ?>>();

		final ColumnConfig<ProductData, String> columnId = new ColumnConfig<ProductData, String>(
				ProductPropertyAccess.INSTANCE.id(), 120, Main.gwtSampleI18n().product_id());
		final ColumnConfig<ProductData, String> columnName = new ColumnConfig<ProductData, String>(
				ProductPropertyAccess.INSTANCE.name(), 120, Main.gwtSampleI18n().product_name());
		final ColumnConfig<ProductData, String> columnProperty1 = new ColumnConfig<ProductData, String>(
				ProductPropertyAccess.INSTANCE.property1(), 120, Main.gwtSampleI18n().product_property1());
		final ColumnConfig<ProductData, String> columnProperty2 = new ColumnConfig<ProductData, String>(
				ProductPropertyAccess.INSTANCE.property2(), 120, Main.gwtSampleI18n().product_property2());
		final ColumnConfig<ProductData, String> columnProperty3 = new ColumnConfig<ProductData, String>(
				ProductPropertyAccess.INSTANCE.property3(), 120, Main.gwtSampleI18n().product_property3());
		final ColumnConfig<ProductData, String> columnProperty4 = new ColumnConfig<ProductData, String>(
				ProductPropertyAccess.INSTANCE.property4(), 120, Main.gwtSampleI18n().product_property4());
		final ColumnConfig<ProductData, List<SupplierDetailData>> columnSuplierDetails = new ColumnConfig<ProductData, List<SupplierDetailData>>(
				ProductPropertyAccess.INSTANCE.supplierDetails(), 300, Main.gwtSampleI18n().product_supplier_details());

		columnConfigs.add(columnId);
		columnConfigs.add(columnName);
		columnConfigs.add(columnProperty1);
		columnConfigs.add(columnProperty2);
		columnConfigs.add(columnProperty3);
		columnConfigs.add(columnProperty4);
		columnConfigs.add(columnSuplierDetails);

		final ColumnModel<ProductData> cmPaging = new ColumnModel<ProductData>(columnConfigs);
		final Grid<ProductData> gridPaging = new Grid<ProductData>(pagingStore, cmPaging);
		gridPaging.setHeight(250);
		new QuickTip(gridPaging);

		basePagingLoader = new DkBasePagingLoader<ProductData, PagingLoadConfig, PagingLoadResult<ProductData>>(pagingStore, Main
				.gwtSampleI18n().action_loading_products(), DkBaseMain.SCREEN_PAGING_SIZE)
		{
			@Override
			protected void doLoad(final PagingLoadConfig pagingLoadConfig,
					final DkBaseAsyncCallback<PagingLoadResult<ProductData>> callback)
			{
				final String filter = controller.getSearchInput();
				controller.setSearchTriggered(false);
				Main.controller().rpcProduct().searchProducts(filter, pagingLoadConfig, callback);
			}
		};
		gridPaging.setLoader(basePagingLoader);

		basePagingLoader.setRemoteSort(true);

		gridPaging.addRowClickHandler(new RowClickHandler()
		{
			@Override
			public void onRowClick(final RowClickEvent event)
			{
				final ProductData productData = pagingStore.get(event.getRowIndex());
				DkLogger.debug(getClass(), "Edit : " + productData);
				controller.getProductEditorController().doEdit(productData);
			}
		});
		setStyleName("fill-background-color");
		setHeadingHtml(Main.gwtSampleI18n().nothing_selected());

		final VerticalLayoutContainer contentPanelLayoutContainer = new VerticalLayoutContainer();
		contentPanelLayoutContainer.add(toolbar, new VerticalLayoutData(1, -1));
		contentPanelLayoutContainer.add(basePagingLoader.getPagingToolbar(), new VerticalLayoutData(1, -1));
		contentPanelLayoutContainer.add(gridPaging, new VerticalLayoutData(1, 1));
		add(contentPanelLayoutContainer);
	}

	@Override
	public boolean needToBeReloaded()
	{
		return isFirstReloadAfterShow() || true;
	}

	@Override
	public void invalidateUi()
	{
		final boolean isLogged = Main.model().isLogged();
		createButton.setEnabled(isLogged);
	}

	@Override
	public DkBaseAction<?> getReloadSubPanelAction(final boolean force, final Object... optionalArgs)
	{
		return basePagingLoader.getReloadAction(controller.isSearchTriggered());
	}

	/**
	 * @return the productStore
	 */
	public ListStore<ProductData> getProductStore()
	{
		return pagingStore;
	}

	/**
	 * @return the input search value
	 */
	public String getSearchInput()
	{
		return textFieldSearch.getValue();
	}
}
